<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.studentmanagement.model.User" %>
<%@ page import="com.studentmanagement.model.Student" %>
<%@ page import="com.studentmanagement.model.Course" %>
<%@ page import="com.studentmanagement.model.Enrollment" %>
<%@ page import="java.util.List" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%
    // 检查用户是否已登录
    User user = (User) session.getAttribute("user");
    if (user == null) {
        response.sendRedirect(request.getContextPath() + "/login");
        return;
    }
    
    // 获取选课记录信息
    Enrollment enrollment = (Enrollment) request.getAttribute("enrollment");
    if (enrollment == null) {
        response.sendRedirect(request.getContextPath() + "/enrollment");
        return;
    }
    
    // 获取学生和课程列表
    List<Student> students = (List<Student>) request.getAttribute("students");
    List<Course> courses = (List<Course>) request.getAttribute("courses");
    
    if (students == null || courses == null) {
        response.sendRedirect(request.getContextPath() + "/enrollment/edit?id=" + enrollment.getEnrollmentId());
        return;
    }
    
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑选课记录 - 学生管理系统</title>
    <link rel="stylesheet" href="<%= request.getContextPath() %>/css/style.css">
</head>
<body>
    <div class="dashboard">
        <header class="header">
            <h1>学生管理系统</h1>
            <div class="user-info">
                欢迎, <%= user.getUsername() %> | <a href="<%= request.getContextPath() %>/logout">退出</a>
            </div>
        </header>
        
        <nav class="sidebar">
            <ul>
                <li><a href="<%= request.getContextPath() %>/student">学生管理</a></li>
                <li><a href="<%= request.getContextPath() %>/course">课程管理</a></li>
                <li><a href="<%= request.getContextPath() %>/enrollment" class="active">选课管理</a></li>
                <li><a href="<%= request.getContextPath() %>/dashboard.jsp">返回控制面板</a></li>
            </ul>
        </nav>
        
        <main class="content">
            <h2>编辑选课记录</h2>
            
            <% if (request.getAttribute("error") != null) { %>
                <div class="error-message">
                    <%= request.getAttribute("error") %>
                </div>
            <% } %>
            
            <form action="<%= request.getContextPath() %>/enrollment/edit" method="post" class="form">
                <input type="hidden" name="enrollmentId" value="<%= enrollment.getEnrollmentId() %>">
                
                <div class="form-group">
                    <label for="studentId">学生:</label>
                    <select id="studentId" name="studentId" required>
                        <option value="">-- 请选择学生 --</option>
                        <% for (Student student : students) { %>
                            <option value="<%= student.getStudentId() %>" <%= student.getStudentId() == enrollment.getStudentId() ? "selected" : "" %>><%= student.getName() %> (<%= student.getMajor() %>)</option>
                        <% } %>
                    </select>
                </div>
                
                <div class="form-group">
                    <label for="courseId">课程:</label>
                    <select id="courseId" name="courseId" required>
                        <option value="">-- 请选择课程 --</option>
                        <% for (Course course : courses) { %>
                            <option value="<%= course.getCourseId() %>" <%= course.getCourseId() == enrollment.getCourseId() ? "selected" : "" %>><%= course.getCourseName() %> (<%= course.getTeacher() %>)</option>
                        <% } %>
                    </select>
                </div>
                
                <div class="form-group">
                    <label for="enrollmentDate">选课日期:</label>
                    <input type="date" id="enrollmentDate" name="enrollmentDate" value="<%= dateFormat.format(enrollment.getEnrollmentDate()) %>" required>
                </div>
                
                <div class="form-group">
                    <label for="grade">成绩:</label>
                    <select id="grade" name="grade">
                        <option value="" <%= enrollment.getGrade() == null || enrollment.getGrade().isEmpty() ? "selected" : "" %>>-- 暂无成绩 --</option>
                        <option value="A" <%= "A".equals(enrollment.getGrade()) ? "selected" : "" %>>A</option>
                        <option value="B" <%= "B".equals(enrollment.getGrade()) ? "selected" : "" %>>B</option>
                        <option value="C" <%= "C".equals(enrollment.getGrade()) ? "selected" : "" %>>C</option>
                        <option value="D" <%= "D".equals(enrollment.getGrade()) ? "selected" : "" %>>D</option>
                        <option value="E" <%= "E".equals(enrollment.getGrade()) ? "selected" : "" %>>E</option>
                        <option value="F" <%= "F".equals(enrollment.getGrade()) ? "selected" : "" %>>F</option>
                    </select>
                </div>
                
                <div class="form-group">
                    <button type="submit" class="btn">保存</button>
                    <a href="<%= request.getContextPath() %>/enrollment" class="btn btn-secondary">取消</a>
                </div>
            </form>
        </main>
        
        <footer class="footer">
            <p>&copy; 2023 学生管理系统. All rights reserved.</p>
        </footer>
    </div>
</body>
</html>

